<%= paginate @variants, theme: "solidus_admin" %>

<table class="index stock-table" id="listing_product_stock">
  <colgroup>
    <col style="width: 50%" />
    <col style="width: 20%" />
    <col style="width: 10%" />
    <col style="width: 5%" />
    <col style="width: 5%" />
    <col style="width: 10%" />
  </colgroup>
  <thead>
    <tr>
      <th><%= Spree.t(:item) %></th>
      <th><%= Spree.t(:options) %></th>
      <th><%= Spree::StockLocation.model_name.human %></th>
      <th><%= Spree.t(:backorderable_header) %></th>
      <th><%= Spree.t(:count_on_hand) %></th>
      <th class="actions"></th>
    </tr>
  </thead>
  <% variants.each do |variant| %>
    <%- locations_without_items = @stock_item_stock_locations - variant.stock_items.flat_map(&:stock_location) %>
    <%- display_add_row = locations_without_items.any? && can?(:create, Spree::StockItem) %>
    <%- row_count = @stock_item_stock_locations.count + 2 %>
    <tbody class="variant-stock-items">
      <tr id="<%= spree_dom_id variant %>">
        <td class="align-center no-padding" rowspan="<%= row_count %>">
          <div class='variant-container'>
            <div class='variant-image'>
              <%= image_tag(variant.display_image(fallback: false).attachment(:small)) %>
            </div>
            <div class='variant-details'>
              <table class='stock-variant-field-table'>
                <tbody>
                  <% @variant_display_attributes.each do |display_attribute| %>
                    <tr>
                      <td><%= Spree.t(display_attribute[:translation_key]) %></td>
                      <td class="fullwidth">
                        <%= variant.send(display_attribute[:attr_name]) %>
                      </td>
                    </tr>
                  <% end %>
                </tbody>
              </table>
            </div>
          </div>
        </td>
        <td class="align-center" rowspan="<%= row_count %>">
          <table class='stock-variant-field-table'>
            <% variant.option_values.sort_by(&:option_type_name).each do |option_value| %>
              <tr>
                <td>
                  <%= option_value.option_type_presentation %>
                </td>
                <td><%= option_value.presentation %></td>
              </tr>
            <% end %>
          </table>
        </td>
      </tr>
      <% variant.stock_items.each do |item| %>
        <% if @stock_item_stock_locations.include?(item.stock_location) %>
          <tr class="js-edit-stock-item stock-item-edit-row" data-variant-id="<%= variant.id %>" data-stock-item="<%= item.to_json %>" data-stock-location-name="<%= item.stock_location.name %>">
            <%# This is rendered in JS %>
          </tr>
        <% end %>
      <% end %>
      <% if display_add_row %>
        <tr class="js-add-stock-item stock-item-edit-row" data-variant-id="<%= variant.id %>">
          <td class='location-name-cell'>
            <%= select_tag :stock_location_id, options_from_collection_for_select(locations_without_items, :id, :name), { :include_blank => true, :class => 'select2', "data-placeholder" => Spree.t(:add_to_stock_location), :id => "variant-stock-location-#{variant.id}" } %>
          </td>
          <td class="align-center">
            <%= check_box_tag :backorderable, 'backorderable', false, id: "variant-backorderable-#{variant.id}" %>
          </td>
          <td class="align-center">
            <%= number_field_tag :count_on_hand, "", class: 'fullwidth', id: "variant-count-on-hand-#{variant.id}" %>
          </td>
          <td class="actions">
            <%= link_to_with_icon 'plus', Spree.t('actions.create'), '#', :no_text => true, :data => {:action => 'green' }, class: "submit" %>
          </td>
        </tr>
      <% end %>
    </tbody>
  <% end %>
</table>

<%= paginate @variants, theme: "solidus_admin" %>
